/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Background;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.Player;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.control.VolumeControl;
/**
 *
 * @author DELL
 */
public class Sound implements Runnable{
    
    private String pathAudio;
    private Player player;
    private boolean loop = true;
    
    public Sound()
    {
        
    }
    
    public Sound(String audio)
    {
        pathAudio = audio;
    }
    
    public void run()
    {
            try {
            playSound();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void playSound() throws IOException, MediaException
    {
        InputStream inputStream = this.getClass().getResourceAsStream(pathAudio); 
        try{
            player = Manager.createPlayer(inputStream,"audio/midi");
            player.prefetch();
            player.setLoopCount(-1);
            player.start();
            
            VolumeControl volumeControl =
                    (VolumeControl) player.getControl("VolumeControl");
            volumeControl.setLevel(100);   
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally{
            inputStream.close();
            inputStream = null;
        }
    }
    
    public void stopPlaySound()
    {
        try {
            if (player != null) {
                player.stop();
                player = null;
            }
        } catch (Exception e) {
        }
    }
    
    
}
